<!--
 * @Descripttion: 
 * @version: @ 1.0.0
 * @Author: 朱江洲
 * @Date: 2022-03-15 16:27:49
 * @LastEditors: 朱江洲
 * @LastEditTime: 2022-05-12 16:43:41
-->
<template>
  <div class="app-container">
    <div class="header">
      <div class="bread">
        <img src="@/assets/images/icon-people.png" alt="" />
        <span style="margin-top: 6px; margin-left: 10px"> 人员信息 </span>
      </div>
      <div class="ipt">
        <el-input
          v-model="input"
          placeholder="请输入姓名/手机号"
          size="normal"
          clearable
          class="ipt-el"
        ></el-input>
        <el-button
          type="primary"
          size="default"
          @click="staffSearch"
          v-model="input"
        >
          搜索
        </el-button>
      </div>
      <div class="btn">
        <el-button type="primary" size="default" @click="newStaff">
          + 新增员工
        </el-button>
        <el-dropdown>
          <el-button class="el-icon-more"></el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="importStaff">
              导入员工
            </el-dropdown-item>
            <el-dropdown-item @click.native="getExl">
              导出员工
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <div class="chooise" v-if="selShow">
      <span>场景：</span>
      <el-select
        v-model="value"
        ref="sel"
        clearable
        @visible-change="(v) => visibleChange(v, 'sel', selClick, admClick)"
        class="sel-sence"
        @change="querySceneList"
      >
        <el-option
          v-for="item in options"
          :key="item.sceneId"
          :label="item.name"
          :value="item.sceneId"
        >
        </el-option>
      </el-select>
      <el-button
        type="primary"
        icon="iconfont icon-shaixuan"
        size="default"
        @click="onScreen"
        style="margin-left: 10px"
      >
        高级筛选
      </el-button>
    </div>
    <div class="sel" v-else>
      <span> 已选中 {{ multipleSelection.length }} 项</span>
      <el-button
        type="primary"
        size="default"
        plain
        :disabled="disabled"
        @click="changeCharge"
      >
        更换负责人
      </el-button>
      <el-button
        type="primary"
        size="default"
        plain
        @click="edit"
        :disabled="disabled"
      >
        编辑
      </el-button>
      <el-button type="danger" size="default" plain @click="del">
        删除
      </el-button>
    </div>
    <div v-if="scenArr !== []" class="scenF">
      <div v-for="(item, index) in scenArr" :key="index" class="scenF-content">
        <div class="scenF-detail" @click="scenSearch(item.sceneId)">
          {{ item.sceneList }}
          <i
            class="el-icon-circle-close"
            @click.stop="scenDel(index)"
            style="margin-left: 10px"
          ></i>
        </div>
      </div>
    </div>
    <div class="table">
      <el-table
        :data="tableData"
        style="width: 100%"
        ref="multipleTable"
        @selection-change="handleSelectionChange"
        :height="tableHight"
        v-loading="loading"
      >
        <template slot="empty" class="emptyBg">
          <img src="@/assets/images/empty.jpg" alt="" />
        </template>
        <el-table-column type="selection" width="55"> </el-table-column>
        <template v-for="(item, index) in switchShow">
          <el-table-column
            :key="index"
            :label="item.label"
            v-if="showH(item, index)"
            :prop="item.stage"
            :width="item.width"
            fixed
          >
            <template slot-scope="scope">
              <span class="staff" @click="drawer(scope.row)">
                {{ scope.row.name }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            :key="index"
            :label="item.label"
            :prop="item.stage"
            :width="item.width"
            v-show="item.value"
            v-if="item.label !== '姓名' && item.value"
          >
          </el-table-column>
        </template>
        <el-table-column fixed="right">
          <template slot="header">
            <i class="iconfont icon-shujupeizhi" @click.stop="tableShow"> </i>
          </template>
        </el-table-column>
      </el-table>
      <div class="wuhu" v-show="dialog.dialogTableVisible">
        <table-sh
          :dialog="dialog"
          :switchShow="switchShow"
          @submit="getSwitchList"
        ></table-sh>
      </div>
    </div>
    <!-- 分页器 -->
    <el-pagination
      prev-text="上一页"
      next-text="下一页"
      background
      :page-size="pageSize"
      :page-sizes="[10, 20, 30, 40, 50, 100]"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      layout=" ->,total,sizes, prev, pager, next, jumper "
      :total="total"
    >
    </el-pagination>
    <staff-drawer
      :dialog="dialog"
      :drawerData="drawerData"
      @putDrawer="putDrawer"
      v-if="dialog.dialogDrawerShowEject"
    ></staff-drawer>
    <new-staff
      :dialog="dialog"
      class="newStaff"
      :putStaff="putStaff"
      ref="newly"
    ></new-staff>
    <div class="importStaff">
      <el-dialog
        title="员工导入"
        :visible.sync="dialogImport"
        width="50%"
        custom-class="dialogClass"
        :close-on-click-modal="false"
      >
        <div class="module">
          <div class="module-txt">
            1.<span style="color: #1267fa">下载</span>员工信息模版，填写模版信息
          </div>
          <div class="module-img">
            <img src="@/assets/images/download.png" alt="" />
            <el-button round style="" @click="downloadTemplate">
              下载模板
            </el-button>
          </div>
        </div>
        <div class="dashed"></div>
        <div class="module">
          <div class="module-txt">
            2.<span style="color: #1267fa">上传</span>填写好的员工信息文件
          </div>
          <div class="module-img">
            <img src="@/assets/images/upload.png" alt="" />
            <el-upload
              class="upload-demo"
              ref="upload"
              action="#"
              :limit="1"
              accept=".xlsx"
              :http-request="httpRequest"
            >
              <el-button class="taskBtn" round>上传文件</el-button>
            </el-upload>
          </div>
        </div>
        <span slot="footer">
          <div>
            <span class="font-txt">
              注：
              导入会覆盖原有员工的信息，如需更新已存在的员工，请先导出通讯录，在导出表格里进行修改
            </span>
          </div>
        </span>
      </el-dialog>
    </div>
    <screen
      :dialog="dialog"
      :switchShow="switchShow"
      @sceneArr="sceneArr"
    ></screen>
    <!-- 更换负责人 -->
    <el-dialog
      title="更换负责人"
      :visible.sync="dialog.dialogPerCha"
      width="50%"
      @close="perClose"
      custom-class="dialogClass"
      :close-on-click-modal="false"
    >
      <charge-person
        v-if="dialog.dialogPerChaEject"
        ref="chaPer"
      ></charge-person>
      <span slot="footer">
        <el-button @click="dialog.dialogPerCha = false"> 取消 </el-button>
        <el-button type="primary" @click="onChangeChar"> 确定 </el-button>
      </span>
    </el-dialog>
    <drag-table
      :dialog="dialog"
      v-if="dialog.senceAdminEject"
      ref="dragTable"
    ></drag-table>
    <tips :dialog="dialog"></tips>
  </div>
</template>

<script>
import {
  getPerson,
  getPersonDetail,
  delPerson,
  importTemplate,
  getExport,
  importExl,
  distriBution,
  bindingPersonLiable,
} from "@/api/registration";
import { queryScene } from "@/api/currency";
import TableSh from "./components/tableSh";
import staffDrawer from "./components/staffDrawer.vue";
import NewStaff from "./components/newStaff.vue";
import Screen from "./components/screen.vue";
import DragTable from "./components/dragTable.vue";
import Tips from "@/components/tips/index.vue";
import { mapGetters } from "vuex";
// 负责人
import chargePerson from "@/components/chargePerson";
export default {
  components: {
    staffDrawer,
    TableSh,
    NewStaff,
    Screen,
    DragTable,
    Tips,
    chargePerson,
  },
  data() {
    return {
      tableHight: window.innerHeight - 330,
      loading: true,
      chargeId: "",
      scenArr: [],
      id: "",
      ids: [],
      charge: "",
      pageSize: 10,
      pageNum: 1,
      defaultParams: {
        label: "name",
        value: "sceneId",
        children: "sceneDataList",
        expandTrigger: "hover",
        checkStrictly: true,
      },
      file: {},
      dialogImport: false,
      putStaff: {},
      selShow: true,
      disabled: false,
      putData: {},
      multipleSelection: [], // 多选数组集合
      pageSize: 10,
      pageNum: 1,
      total: 0,
      input: "", //搜索
      value: "", // 场景的
      drawerData: {}, // 抽屉回显数据
      dialog: {
        dialogDrawerShow: false,
        dialogDrawerShowEject: false,
        dialogTableVisible: false,
        dialogStaffVisble: false,
        dialogScreenVisible: false,
        dialogPerCha: false,
        dialogPerChaEject: false,
        senceAdmin: false,
        senceAdminEject: false,
        isVipTip: false,
      },
      chargeList: [],
      // 控制表单的显示与隐藏
      switchShow: [
        {
          value: true,
          label: "姓名",
          stage: "name",
        },
        {
          value: true,
          label: "手机号",
          stage: "phoneNumber",
          width: "150px",
        },
        {
          value: true,
          label: "性别",
          stage: "sex",
        },
        {
          value: true,
          label: "公司",
          stage: "companyName",
          width: "180px",
        },
        {
          value: true,
          label: "部门",
          stage: "deptName",
        },
        {
          value: true,
          label: "身份证号",
          stage: "idNumber",
          width: "220px",
        },
        {
          value: true,
          label: "生日",
          stage: "birthday",
          width: "140px",
        },
        {
          value: true,
          label: "文化程度",
          stage: "education",
        },
        {
          value: true,
          label: "工作状态",
          stage: "jobState",
        },
        {
          value: true,
          label: "负责人",
          stage: "userName",
        },
        {
          value: true,
          label: "最后更新时间",
          stage: "uploadTime",
          width: "120px",
        },
        {
          value: true,
          label: "创建人",
          stage: "createBy",
        },
        {
          value: true,
          label: "创建时间",
          stage: "createTime",
        },
      ],
      tableData: [],
      options: [],
    };
  },
  created() {
    this.getPersonData();
    this.getScene();
    this.onCharge();
  },
  watch: {
    input(newValue, oldValue) {
      if (newValue == "") {
        this.getPersonData();
      }
    },
    file: {
      deep: true,
      handler(val) {
        if (val !== {}) {
          var fd = new FormData();
          fd.append("file", this.file);
          fd.append("updateSupport", "true");
          importExl(fd).then((res) => {});
        }
      },
    },
  },
  mounted() {
    document.addEventListener("click", this.bodyCloseMenus);
    window.onresize = () => {
      return (() => {
        this.tableHight = window.innerHeight - 330;
      })();
    };
  },
  beforeDestroy() {
    document.removeEventListener("click", this.bodyCloseMenus);
  },
  activated() {
    this.tableHight = window.innerHeight - 330;
  },
  computed: {
    ...mapGetters(["isVip"]),
  },
  methods: {
    // 联系客服
    qrShow() {
      console.log(1);
      this.dialog.csQrShow = true;
      console.log(this.dialog.csQrShow);
    },
    // 点击空白
    bodyCloseMenus(e) {
      let self = this;
      if (self.dialog.dialogTableVisible == true) {
        self.dialog.dialogTableVisible = false;
      }
    },
    // 点击临时搜索
    scenSearch(id) {
      this.getPersonData(id);
    },
    // 删除临时搜索
    scenDel(i) {
      this.scenArr.splice(i, 1);
      if (this.scenArr.length == 0) {
        this.getPersonData();
      }
    },
    sceneArr(data) {
      this.scenArr.push(data);
    },
    //管理
    admClick() {
      this.dialog.senceAdminEject = true;
      this.$nextTick(function () {
        this.dialog.senceAdmin = true;
      });
    },
    //新建场景
    selClick() {
      this.onScreen();
    },
    // 场景下拉框按钮
    visibleChange(visible, refName, onClick, onClick2) {
      if (visible) {
        const ref = this.$refs[refName];
        let popper = ref.$refs.popper;
        if (popper.$el) popper = popper.$el;
        if (
          !Array.from(popper.children).some(
            (v) => v.className === "el-cascader-menu__list"
          )
        ) {
          const el = document.createElement("ul");
          el.className = "el-cascader-menu__list";
          el.style =
            "border-top: solid 1px #E4E7ED; padding:0; color: #606266;";
          el.innerHTML = `<li class="el-cascader-node" >
                            <span class="el-cascader-node__label" style='color:#1267FA'>新建场景</span>
                            </li>
                        `;
          const el2 = document.createElement("ul");
          el2.className = "el-cascader-menu__list";
          el2.innerHTML = `<li class="el-cascader-node" >
                            <span class="el-cascader-node__label" style='color:#1267FA'>管理</span>
                            </li>
                        `;
          popper.appendChild(el);
          popper.appendChild(el2);
          el2.onclick = () => {
            onClick2 && onClick2();
            if (ref.toggleDropDownVisible) {
              ref.toggleDropDownVisible(false);
            } else {
              ref.visible = false;
            }
          };
          el.onclick = () => {
            // 底部按钮的点击事件 点击后想触发的逻辑也可以直接写在这
            onClick && onClick();
            // 以下代码实现点击后弹层隐藏 不需要可以删掉
            if (ref.toggleDropDownVisible) {
              ref.toggleDropDownVisible(false);
            } else {
              ref.visible = false;
            }
          };
        }
      }
    },
    querySceneList() {
      this.getPersonData(this.value);
    },
    putDrawer(val) {
      this.putStaff = val;
      console.log(val);
    },
    staffSearch() {
      this.getPersonData();
    },
    // 更换负责人
    changeCharge() {
      this.dialog.dialogPerChaEject = true;
      this.$nextTick(function () {
        this.dialog.dialogPerCha = true;
      });
    },
    // 关闭更换负责人
    perClose() {
      this.charge = "";
      this.dialog.dialogPerChaEject = false;
    },
    onChangeChar() {
      if (this.charge == "") return this.$message.error("请选择负责人");
      bindingPersonLiable({
        id: this.chargeId,
        userId: this.charge,
      }).then((res) => {
        if (res.code != 200) return this.$message.error("更换失败");
        this.$message.success(res.msg);
        this.getPersonData();
        this.dialog.dialogPerCha = false;
      });
    },
    onCharge() {
      distriBution().then((res) => {
        this.chargeList = res.data;
      });
    },

    // 下载模板
    downloadTemplate() {
      importTemplate().then((response) => {
        this.download(response.msg);
      });
    },
    /** 导出按钮操作 */
    getExl() {
      this.$confirm("是否确认导出所有角色数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return getExport();
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
    // 请求场景
    getScene() {
      queryScene({
        type: 2,
      }).then((res) => {
        if (res.default !== null) {
          res.scenes.unshift(res.default);
          this.options = res.scenes;
        } else {
          this.options = res.scenes;
        }
      });
    },
    onScreen() {
      this.dialog.dialogScreenVisible = true;
    },
    httpRequest(data) {
      this.file = data.file;
    },
    importStaff() {
      this.dialogImport = true;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageNum = 1;
      this.getPersonData();
    },
    //currentPage 改变时会触发
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getPersonData();
    },
    async edit() {
      this.dialog.dialogStaffVisble = true;
      if (this.multipleSelection.length !== 0) {
        this.id = this.multipleSelection[0].id;
      }
      getPersonDetail(this.id).then((res) => {
        this.putStaff = res.data;
        if (this.putStaff.idPositive) {
          this.$refs.newly.fileList = [
            {
              name: "",
              url: this.putStaff.idPositive,
            },
          ];
        }
        if (this.putStaff.idBack) {
          this.$refs.newly.fileList1 = [
            {
              name: "",
              url: this.putStaff.idBack,
            },
          ];
        }
        if (this.putStaff.idPhoto) {
          this.$refs.newly.fileList2 = [
            {
              name: "",
              url: this.putStaff.idPhoto,
            },
          ];
        }

        if (this.putStaff.educationPhoto) {
          this.$refs.newly.fileList3 = [
            {
              name: "",
              url: this.putStaff.educationPhoto,
            },
          ];
        }
        if (this.putStaff.fileName !== null && this.putStaff.fileName !== "") {
          this.$refs.newly.fileList4 = [
            {
              name: this.putStaff.fileName,
              url: this.putStaff.fileId,
            },
          ];
        }
      });
    },
    change() {},
    async del() {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该职位, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      /*如果用户确认打印confirm,如果用户取消显示cancel*/
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除!");
      } else {
        let res = await delPerson(this.ids);
        this.getPersonData();
        return this.$message.success("删除成功！");
      }
    },
    async getPersonData(val) {
      let res = await getPerson({
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        sceneId: val,
        identification: this.input,
      });
      res.rows.map((item) => {
        if (item.sex == 1) {
          item.sex = "女";
        } else if (item.sex == 2) {
          item.sex = "未知";
        } else if (item.sex == 0) {
          item.sex = "男";
        }
        if (item.jobState == 1) {
          item.jobState = "在职";
        } else if (item.jobState == 2) {
          item.jobState = "离职";
        }
        if (item.education == 1) {
          item.education = "小学";
        } else if (item.education == 2) {
          item.education = "初中";
        } else if (item.education == 3) {
          item.education = "高中/中专";
        } else if (item.education == 4) {
          item.education = "专科";
        } else if (item.education == 5) {
          item.education = "大学";
        } else if (item.education == 6) {
          item.education = "研究生";
        }
      });
      this.tableData = res.rows;
      this.total = res.total;
      this.loading = false;
    },
    getSwitchList(list) {
      this.$set(this, "switchShow", list);
    },
    updataSubmit(data) {
      this.switchShow = data;
    },
    async drawer(row) {
      let res = await getPersonDetail(row.id);
      this.drawerData = res.data;
      this.dialog.dialogDrawerShowEject = true;
      this.$nextTick(function () {
        this.dialog.dialogDrawerShow = true;
      });
    },
    newStaff() {
      // this.$store.dispatch("isVip");

      // if (this.isVip) {
      //   this.dialog.dialogStaffVisble = true;
      // } else {
      //   this.dialog.isVipTip = true;
      // }
      this.dialog.dialogStaffVisble = true;
    },
    handleChange(val) {
      this.value = val[0];
      this.getPersonData(val[0], this.input);
    },
    // 表格前多选器
    handleSelectionChange(val) {
      if (val.length !== 0) {
        this.chargeId = val[0].id;
      }
      this.ids = val.map((item) => item.id);
      this.selShow = false;
      this.multipleSelection = val;
      if (val.length == 0) {
        this.selShow = true;
      }
      if (val.length > 1) {
        this.disabled = true;
      } else {
        this.disabled = false;
      }
    },
    tableShow() {
      if (this.dialog.dialogTableVisible == false) {
        this.dialog.dialogTableVisible = true;
      } else {
        this.dialog.dialogTableVisible = false;
      }
    },
    showH(item, index) {
      let bole = false;
      if (item.label === "姓名") {
        bole = true;
      }
      if (!item.value) bole = false;
      return bole;
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  width: 100%;
  height: 74px;
  display: flex;
  justify-content: space-between;
  border-bottom: 1px solid #ececec;
  .bread {
    margin-top: 20px;
    margin-left: 5px;
    height: 30px;
    display: flex;
    vertical-align: middle;
    p {
      margin-left: 10px;
      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #2b2b2b;
    }
  }

  .ipt {
    margin-top: 16px;
    width: 340px;
    /deep/ .el-input__inner {
      border-radius: 4px 0 0 4px;
    }
    .el-button--primary {
      border-radius: 0 4px 4px 0;
    }
  }
  .btn {
    margin-top: 16px;
    .el-button--primary {
      background-color: #ed732e;
      border: #ed732e;
    }
  }
}
.el-dropdown {
  margin-left: 15px;
}
.chooise {
  margin-top: 20px;
  .sel {
    margin: 0 14px;
  }
  .el-button {
    font-size: 14px;
    /deep/ span {
      margin-left: 5px;
    }
  }
}
/deep/.el-table__body-wrapper {
  height: 695px;
}
.staff {
  color: #1267fa;
}
.el-table {
  margin-top: 20px;
}
.newStaff {
  margin-left: 200px;
}
/deep/.el-tabs__content {
  height: 100vh;
}
.el-pagination {
  margin-top: 24px;
  /deep/ .el-pagination__total {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    margin-right: 25px;
  }
  /deep/ .btn-prev {
    width: 80px;
    height: 40px;
    text-align: center;
  }
  /deep/ .btn-next {
    width: 80px;
    height: 40px;
    text-align: center;
  }
  /deep/ .number {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .el-input__inner {
    // width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .more {
    width: 40px;
    height: 40px;
    line-height: 40px;
    text-align: center;
  }
  /deep/ .el-pagination__jump {
    .el-input__inner {
      width: 40px;
      height: 40px;
    }
  }
}
.sel {
  margin-top: 20px;
  span {
    margin-right: 40px;
  }
}
.font-txt {
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #9aa1af;
  line-height: 17px;
}
.importStaff {
  /deep/.el-dialog__footer {
    text-align: left;
  }
  /deep/ .el-dialog__body {
    display: flex;
    justify-content: space-around;
    .module {
      width: 40%;
      height: 300px;
      .module-img {
        width: 49px;
        height: 60px;
        margin: 50%;
        transform: translate(-50%, -50%);
        img {
          width: 100%;
          height: 100%;
        }
        .el-button {
          margin-top: 10px;
          transform: translateX(-25%);
        }
      }

      .module-txt {
        transform: translateY(400%);
        text-align: center;
        font-size: 14px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #1f2e4d;
      }
    }
  }
  .dashed {
    width: 1px;
    height: 180px;
    border-left: 2px dashed rgba(0, 0, 0, 0.1);
    transform: translateY(35%);
  }

  /deep/.el-upload-list {
    display: none;
  }
}
.ipt-el {
  width: 240px;
}
.table {
  position: relative;
  .wuhu {
    position: absolute;
    top: 45px;
    right: 0px;
    z-index: 9999;
    background-color: #fff;
    border: 1px solid #ececec;
  }
}
.scenF {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  .scenF-content {
    display: flex;
    margin-left: 10px;
    margin-top: 10px;
    .scenF-detail {
      height: 36px;
      line-height: 36px;
      padding-left: 20px;
      padding-right: 20px;
      border: 1px solid #9aa1af;
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #666666;
      .el-icon-circle-close {
        transform: translateY(3px);
      }
    }
  }
}
</style>